package com.h117121.fashionshop.controller.admin;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import com.h117121.fashionshop.controller.generic.GenericController;
import com.h117121.fashionshop.core.dom.AdsImageForm;
import com.h117121.fashionshop.validator.AdsValidator;

@Controller
@RequestMapping("/admin/ads")
public class AdminAdsController extends GenericController {
	@Autowired
	private MessageSource messageSource;

	@Autowired
	private AdsValidator validator;

	@RequestMapping(method = RequestMethod.GET)
	protected String showPage(ModelMap model) {
		model.addAttribute("adsForm", new AdsImageForm());
		return "AdminAdvertisementPage";
	}

	@RequestMapping(method = RequestMethod.POST)
	protected String editProduct(HttpServletRequest request,
			@ModelAttribute(value = "adsForm") AdsImageForm adsForm,
			BindingResult errors, ModelMap model) {
		validator.validate(adsForm, errors);
		if (errors.hasErrors()) {
			return "AdminAdvertisementPage";
		}

		MultipartFile file = adsForm.getImageLeft();

		if (null != file && file.getSize() != 0) {
			String path = request.getSession().getServletContext()
					.getRealPath("/");
			path += "/images/ads/1.jpg";
			System.out.println(path);
			try {
				InputStream inputStream = file.getInputStream();
				OutputStream outputStream = new FileOutputStream(path);
				int readBytes = 0;
				byte[] buffer = new byte[10000];
				while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {
					outputStream.write(buffer, 0, readBytes);
				}
				outputStream.close();

			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		file = adsForm.getImageRight();

		if (null != file && file.getSize() != 0) {
			String path = request.getSession().getServletContext()
					.getRealPath("/");
			path += "/images/ads/2.jpg";
			System.out.println(path);
			try {
				InputStream inputStream = file.getInputStream();
				OutputStream outputStream = new FileOutputStream(path);
				int readBytes = 0;
				byte[] buffer = new byte[10000];
				while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {
					outputStream.write(buffer, 0, readBytes);
				}
				outputStream.close();

			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		model.addAttribute("successfulMessage", true);
		return "AdminAdvertisementPage";
	}
}